﻿using Microsoft.EntityFrameworkCore;
using StudentManagement.Extensions.Pagenation;
using StudentManagement.Infrastructure.Repositories;
using StudentManagement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace StudentManagement.Services
{
    public class TeacherService : ITeacherService
    {
        private readonly IRepository<Teacher> teacherRepository;

        public TeacherService(IRepository<Teacher> teacherRepository)
        {
            this.teacherRepository = teacherRepository;
        }

        public Extensions.Pagenation.PagedData<Teacher> GetPagedData(PageParameters pageParameters)
        {
            var data = teacherRepository.GetAll();
            if (!String.IsNullOrEmpty(pageParameters.SortBy)) {
                data = data.OrderBy(pageParameters.SortBy);
            }
            if (!String.IsNullOrEmpty(pageParameters.SearchString)) {
                data = data.Where(t=>t.Name.Contains(pageParameters.SearchString) || t.Department.Contains(pageParameters.SearchString));
            }

            var model = data.Include(t => t.TeacherCourses)
                            .ThenInclude(c => c.Course)
                            .AsNoTracking()
                            .ToList();                                

            return new PagedData<Teacher>(model, pageParameters.PageIndex, pageParameters.PageSize);
        }
    }
}
